Thread: inet_aton()... Noob needs help with sockets :|

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194

    inet_aton()... Noob needs help with sockets :|

    Hi dee Ho again.

    I started learning how to toy with sockets yesterday... Well, I am utterly confused with the structures && functions dealing with ports & hostnames, with client & server sockets, with this & that

    Basically what I would like to do is connect to certain server(s) & be able to send & receive data. I've done that with php sockets, so I assume I should be able to do it with c++ too

    I prepared following class for that:
    connect.h:

    Code:
    #ifndef _CONNECT
    #define _CONNECT
    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    using namespace std;
    
    class Connect {
    
    private:
    
            int sock;
            struct sockaddr_in serv_addr;
    
    public:
            Connect(){;}
            ~Connect()
                    {
                    close(this->sock);
                    }
            int con(string host, int port); //open socket
            int send_command(string command);
            int send_payload(string command);
            char *receive();
    };
    
    #endif
    and connect.cpp:
    Code:
    #include "connect.h"
    #include <iostream>
    #include <string.h>
    #include <errno.h>
    #include <string.h>
    #include <stdio.h>
    #include <arpa/inet.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <ctype.h>
    #include <stdlib.h>
    #include <netdb.h>
    
    using namespace std;
    
    int Connect::con(string host, int port)
    {
            struct hostent *h;
    
    
    
            char *ip;
    
            //int success;
    
    
            if((h=gethostbyname(host.c_str()))==NULL)
            {
                    herror("gethostbyname");
                    exit(1);
            }
    
            ip=inet_ntoa(*((struct in_addr *)h->h_addr));
    
    
            if((this->sock = socket(AF_INET, SOCK_STREAM, 0)) < 0 )   //Lets make the filedescriptor
            {
                    cout << "Smelly Socks!" << endl;
                    exit(-666);
            }
    
            /*Lets connect to server*/
            serv_addr.sin_family = AF_INET;
            if( inet_aton(ip,&serv_addr.sin_addr) !=0 )
            {
                    cout << "invalid ip format: " << ip << endl;
                    exit(1);
            }
            serv_addr.sin_port = htons(port);
    
            if(connect(sock,(struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
            {
                    cout << "Connecting to " << ip << " failed" << endl;
                    exit(-666);
            }
            return 1;
    }
    
    int Connect::send_command(string command)
    {
            int len;
            len=strlen(command.c_str())+2;
    
            command=command+"\r\n";
            write(sock,command.c_str(),len);
            cout << "SENT : " << command << endl;
            return 1;
    }
    
    int Connect::send_payload(string command)
    {
            int len;
            len=strlen(command.c_str());
            cout << "SENT : " << command << endl;
    
            write(sock,command.c_str(),len);
            return 1;
    
    }
    
    char *Connect::receive()
    {
            char *buf;
            int nread;
            nread=read(this->sock,buf,1024);
            cout << "GOT : " << buf << endl;
            return buf;
    }

    Now when I try to use it to connect to a certain server & send command inet_aton fails (it returns something that's not 0, and according to man pages it should not.)

    (this
    Code:
     if( inet_aton(ip,&serv_addr.sin_addr) !=0 )
            {
                    cout << "invalid ip format: " << ip << endl;
                    exit(1);
            }
    Any ideas what's wrong? And can use this kind of class to do the connection? As I said, I am a noob with sockets, and the structures & handling of ip's / hostnames are mostly just taken from examples...

    If someone feels like explaining them (structures & functions to manipulate hostnames/ips) well, feel free, (that would really help me understanding them better, cause the tutos I found just stated there is such structures, but the purpose is not terribly clear :/) but I would also be gratefull if someone just could tell me why inet_aton does not work.

    Oh, BTW. I am using linux & g++ compiler.
    Last edited by Maz; 11-22-2005 at 03:27 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer over sockets
    By adrians in forum C Programming
    Replies: 3
    Last Post: 03-02-2009, 12:56 AM
  2. Best way to poll sockets?
    By 39ster in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-22-2008, 01:43 PM
  3. multiple UDP sockets with select()
    By nkhambal in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-17-2006, 07:36 PM
  4. Raw Sockets and SP2...
    By Devil Panther in forum Networking/Device Communication
    Replies: 11
    Last Post: 08-12-2005, 04:52 AM
  5. Starting window sockets
    By _Cl0wn_ in forum Windows Programming
    Replies: 2
    Last Post: 01-20-2003, 11:49 AM